package com.example.loginexample;

import com.example.db.DataBaseAdapter;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class SignUpActivity extends Activity {

    EditText etUsername, etPassword, etConfirmPassword;
    Button btnCreateAcc;
    
    DataBaseAdapter dbAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.signup);
        
        // create instance of db
        dbAdapter = new DataBaseAdapter(this);
        dbAdapter = dbAdapter.open();
        
        // get reference of views
        etUsername = (EditText)findViewById(R.id.etUsername);
        etPassword = (EditText)findViewById(R.id.etPassword);
        etConfirmPassword = (EditText)findViewById(R.id.etConfirmPwd);
        
        btnCreateAcc = (Button)findViewById(R.id.btnCreateAcc);
        
        btnCreateAcc.setOnClickListener(new View.OnClickListener() {
            
            @Override
            public void onClick(View v) {
                String userName = etUsername.getText().toString();
                String passWord = etPassword.getText().toString();
                String confirmPwd = etConfirmPassword.getText().toString();
                
                // check null or blank 
                if(userName.equals("") || passWord.equals("") || confirmPwd.equals("")) {
                    Toast.makeText(getApplicationContext(), "Please fill data for all field", Toast.LENGTH_LONG).show();
                    return;
                }
                if(!passWord.equals(confirmPwd)) {
                    Toast.makeText(getApplicationContext(), "Password does not match!!!", Toast.LENGTH_LONG).show();
                    return;
                } else {
                    // save in database
                    dbAdapter.insertEntry(userName, passWord);
                    Toast.makeText(getApplicationContext(), "Account's created successfull", Toast.LENGTH_LONG).show();
                }
                
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        dbAdapter.close();
    }

    
}
